Skip to content

fix(Segments): Restore segment list performance on large installations - #8587

Merged
emyller merged 6 commits into
mainfrom
fix(segments)/has-overrides-full-table-scan
Sep 23, 2026
Merged

emyller merged 6 commits into
mainfrom
fix(segments)/has-overrides-full-table-scan

Conversation

@matthewelwell

@matthewelwell matthewelwell commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for submitting a PR! Please check the boxes below:

  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Since #8548, get_all_live_or_scheduled_overrides compared a feature state's version against EnvironmentFeatureVersion.objects.get_live_or_scheduled() — an uncorrelated subquery over every published version in the installation. Correlating the check to the feature state's own feature and environment lets the existing efv_env_feature_pub_created index serve it, so there is no migration.

How did you test this code?

EXPLAIN (ANALYZE) on a production read replica, listing segments for a project with 156 of them.

Before — 59,069 ms. SubPlan 1 is the version check, reading ~90k and ~147k version rows, 313 times over. Note also features_featuresegment scanned by primary key, and features_featurestate gathered across the whole table:

Index Scan using segments_segment_project_id on segments_segment (actual time=526.402..59068.949 rows=156 loops=1)
  SubPlan 2
    ->  Merge Semi Join (actual time=378.375..378.396 rows=1 loops=156)
          ->  Index Scan using features_featuresegment_pkey (actual time=43.666..43.666 rows=1 loops=156)
                Filter: (segment_id = segments_segment.id)
                Rows Removed by Filter: 112198
          ->  Gather Merge (actual time=63.223..327.546 rows=98349 loops=156)
                ->  Nested Loop Left Join (actual time=0.091..219.699 rows=33110 loops=468)
                      ->  Parallel Index Scan using features_featurestate_feature_segment_id (rows=37767 loops=468)
                      SubPlan 1
                        ->  Merge Anti Join (actual time=0.108..156.984 rows=45721 loops=313)
                              ->  Index Scan using efv_env_feature_pub_created v0 (rows=90105 loops=313)
                              ->  Index Scan using efv_env_feature_pub_created u0 (rows=147469 loops=313)
Execution Time: 59069.494 ms

After — 3.6 ms. SubPlan 1 is now an index seek and is never reached; the other two joins flip to nested loops on the right indexes:

Limit (actual time=0.600..3.335 rows=156 loops=1)
  ->  Index Scan using segments_segment_project_id on segments_segment (rows=156 loops=1)
  SubPlan 2
    ->  Nested Loop Semi Join (actual time=0.018..0.018 rows=1 loops=156)
          ->  Index Scan using features_featuresegment_segment_id (actual time=0.004..0.004 rows=1 loops=156)
                Index Cond: (segment_id = segments_segment.id)
          ->  Nested Loop Left Join (actual time=0.013..0.013 rows=1 loops=156)
                ->  Index Scan using features_featurestate_feature_segment_id (rows=1 loops=156)
                      Index Cond: (feature_segment_id = w0.id)
                SubPlan 1
                  ->  Index Scan using efv_env_feature_pub_created (never executed)
                        Index Cond: ((environment_id = v0.environment_id) AND (feature_id = v0.feature_id))
Execution Time: 3.622 ms

The existing get_all_live_or_scheduled_overrides tests cover the semantics and pass unchanged. The new test asserts how many version rows the plan reads: 1 with this change, 4,002 without.

@matthewelwell
matthewelwell requested a review from a team as a code owner September 23, 2026 18:52
@matthewelwell
matthewelwell requested review from emyller and removed request for a team September 23, 2026 18:52
@vercel

vercel Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

3 Skipped Deployments
Project Deployment Actions Updated
docs Ignored Ignored Preview Sep 23, 2026 9:37pm UTC
flagsmith-frontend-preview Ignored Ignored Preview Sep 23, 2026 9:37pm UTC
flagsmith-frontend-staging Ignored Ignored Preview Sep 23, 2026 9:37pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: ecb3716d-7c1d-46fa-a53a-94094e3e377d

📥 Commits

Reviewing files that changed from the base of the PR and between b77585a and 89534b8.

📒 Files selected for processing (1)
  • api/segments/services.py

Included review availability: Your plan provides up to 8 included reviews per hour; 6 remain after this review.


📝 Walkthrough

Walkthrough

The v2 filter in get_all_live_or_scheduled_overrides now checks for a later published version of the same feature and environment whose live_from is no later than the current time. It excludes the current version when such a version exists. The v1 filtering path is unchanged.

Estimated code review effort: 2 (Simple) | ~8 minutes

Merge Risk: ⚪ Minimal · up to 89534

No actionable merge-blocking risk is established by the supplied review context; the change is ready to merge subject to normal checks.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added api Issue related to the REST API fix labels Sep 23, 2026
@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Docker builds report

Image Build Status Security report
ghcr.io/flagsmith/flagsmith-e2e:pr-8587 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-api-test:pr-8587 Finished ✅ Skipped
ghcr.io/flagsmith/flagsmith-frontend:pr-8587 Finished ✅ Results ✅
ghcr.io/flagsmith/flagsmith-api:pr-8587 Finished ✅ Results ✅
ghcr.io/flagsmith/flagsmith-private-cloud:pr-8587 Finished ✅ Results ✅

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 3281cb46-4638-4056-97c2-506c18c4df75

📥 Commits

Reviewing files that changed from the base of the PR and between 3e5fa33 and 2f444dd.

📒 Files selected for processing (2)
  • api/segments/services.py
  • api/tests/unit/segments/test_unit_segments_services.py

Included review availability: Your plan provides up to 8 included reviews per hour; 6 remain after this review.

Comment thread api/tests/unit/segments/test_unit_segments_services.py Outdated
@codecov

codecov Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.80%. Comparing base (3e5fa33) to head (89534b8).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8587      +/-   ##
==========================================
- Coverage   98.81%   98.80%   -0.01%     
==========================================
  Files        1631     1631              
  Lines       67012    67013       +1     
==========================================
- Hits        66215    66214       -1     
- Misses        797      799       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor
✅ private-cloud · depot-ubuntu-latest-arm-16 — run #20766 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)

passed  4 passed

Details

stats  4 tests across 3 suites
duration  1 minute, 3 seconds
commit  89534b8
info  🔄 Run: #20766 (attempt 1)

🗂️ Previous results
✅ private-cloud · depot-ubuntu-latest-16 — run #20766 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-16)

passed  4 passed

Details

stats  4 tests across 4 suites
duration  18 seconds
commit  89534b8
info  🔄 Run: #20766 (attempt 1)

✅ oss · depot-ubuntu-latest-16 — run #20766 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-16)

passed  1 passed

Details

stats  1 test across 1 suite
duration  32.1 seconds
commit  89534b8
info  🔄 Run: #20766 (attempt 1)

✅ oss · depot-ubuntu-latest-arm-16 — run #20766 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-arm-16)

passed  18 passed
skipped  1 skipped

Details

stats  19 tests across 12 suites
duration  1 minute, 8 seconds
commit  89534b8
info  🔄 Run: #20766 (attempt 1)

Skipped tests

firefox › tests/onboarding-tests.pw.ts › Onboarding › New user connects via the single-page onboarding flow @oss

✅ private-cloud · depot-ubuntu-latest-arm-16 — run #20765 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)

passed  4 passed

Details

stats  4 tests across 3 suites
duration  38.5 seconds
commit  b77585a
info  🔄 Run: #20765 (attempt 1)

✅ private-cloud · depot-ubuntu-latest-16 — run #20765 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-16)

passed  3 passed

Details

stats  3 tests across 3 suites
duration  33.4 seconds
commit  b77585a
info  🔄 Run: #20765 (attempt 1)

✅ oss · depot-ubuntu-latest-arm-16 — run #20765 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-arm-16)

passed  1 passed

Details

stats  1 test across 1 suite
duration  47.1 seconds
commit  b77585a
info  🔄 Run: #20765 (attempt 1)

✅ oss · depot-ubuntu-latest-16 — run #20765 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-16)

passed  1 passed

Details

stats  1 test across 1 suite
duration  34.8 seconds
commit  b77585a
info  🔄 Run: #20765 (attempt 1)

✅ private-cloud · depot-ubuntu-latest-arm-16 — run #20764 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)

passed  3 passed

Details

stats  3 tests across 2 suites
duration  12.2 seconds
commit  53ae4aa
info  🔄 Run: #20764 (attempt 1)

✅ private-cloud · depot-ubuntu-latest-16 — run #20764 (attempt 1)

Playwright Test Results (private-cloud - depot-ubuntu-latest-16)

passed  2 passed

Details

stats  2 tests across 2 suites
duration  39.6 seconds
commit  53ae4aa
info  🔄 Run: #20764 (attempt 1)

✅ oss · depot-ubuntu-latest-16 — run #20764 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-16)

passed  3 passed

Details

stats  3 tests across 3 suites
duration  11.4 seconds
commit  53ae4aa
info  🔄 Run: #20764 (attempt 1)

✅ oss · depot-ubuntu-latest-arm-16 — run #20764 (attempt 1)

Playwright Test Results (oss - depot-ubuntu-latest-arm-16)

passed  2 passed

Details

stats  2 tests across 2 suites
duration  4.4 seconds
commit  53ae4aa
info  🔄 Run: #20764 (attempt 1)

@matthewelwell
matthewelwell marked this pull request as draft September 23, 2026 19:00
@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Visual Regression

19 screenshots compared. See report for details.
View full report

`get_all_live_or_scheduled_overrides` matched a feature state's version
against `EnvironmentFeatureVersion.objects.get_live_or_scheduled()`, an
uncorrelated subquery over every published version in the installation.
On a reported project, the `has_overrides` annotation on the segment
list took 59 seconds to return 156 rows, almost all of it spent in a
merge anti-join reading ~90k and ~147k version rows, 313 times over.

Correlate the superseded-version check with the feature state's own
feature and environment instead, which the existing
`efv_env_feature_pub_created` index already covers. The same query then
runs in 3.6ms, and the planner drops the merge joins that scanning
drove it to, reading `features_featuresegment` by segment rather than
by primary key.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@matthewelwell
matthewelwell force-pushed the fix(segments)/has-overrides-full-table-scan branch from 2f444dd to 07463d5 Compare September 23, 2026 20:43

@emyller emyller left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be fair, I still don't grasp why the filter needs to be not superseded instead of superseding, but the logic here looks sound.

Comment thread api/segments/services.py
Comment thread api/tests/unit/segments/test_unit_segments_services.py Outdated
@github-actions github-actions Bot added fix and removed fix labels Sep 23, 2026
Comment thread api/segments/services.py Outdated
@github-actions github-actions Bot added fix and removed fix labels Sep 23, 2026
Comment thread api/tests/unit/segments/test_unit_segments_services.py Outdated
Comment thread api/tests/unit/segments/test_unit_segments_services.py Outdated
Comment thread api/tests/unit/segments/test_unit_segments_services.py Outdated
Comment thread api/tests/unit/segments/test_unit_segments_services.py Outdated
Comment thread api/tests/unit/segments/test_unit_segments_services.py Outdated
Comment thread api/tests/unit/segments/test_unit_segments_services.py Outdated
Drop the module-level constant and the helper's return value, and assert
the exact number of version rows the plan reads rather than a fraction of
the table's size.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added fix and removed fix labels Sep 23, 2026
@matthewelwell
matthewelwell marked this pull request as ready for review September 23, 2026 21:07
@github-actions github-actions Bot added fix and removed fix labels Sep 23, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 18c1b18a-4fb3-4780-9abf-7353a7ba6a01

📥 Commits

Reviewing files that changed from the base of the PR and between 2f444dd and 53ae4aa.

📒 Files selected for processing (2)
  • api/segments/services.py
  • api/tests/unit/segments/test_unit_segments_services.py

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Comment thread api/tests/unit/segments/test_unit_segments_services.py Outdated
@github-actions github-actions Bot added fix and removed fix labels Sep 23, 2026
@github-actions github-actions Bot added fix and removed fix labels Sep 23, 2026
@emyller
emyller merged commit b10c6b3 into main Sep 23, 2026
34 checks passed
@emyller
emyller deleted the fix(segments)/has-overrides-full-table-scan branch September 23, 2026 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants